package de.duester.genealogy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import de.duester.genealogy.adapter.EventAttributeListAdapter;
import de.duester.genealogy.model.Event;
import de.duester.genealogy.model.EventAttribute;
import de.duester.genealogy.model.EventDate;
import de.duester.genealogy.model.EventDocument;
import de.duester.genealogy.model.EventPlace;
import de.duester.genealogy.model.EventString;
import de.duester.genealogy.model.Person;
import de.duester.genealogy.model.PersonEvent;
import de.duester.genealogy.model.Place;
import de.duester.genealogy.model.service.EventService;

public class PersonEventEditActivity extends Activity implements OnClickListener {
	private TextView tvPerson;
	private TextView tvType;
	private ListView lvAttributes;
	private Button btnSave;

	private long eventId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_person_event_edit);
		init();
	}

	private void init() {
		long personId = getIntent().getLongExtra("personId", -1);
		Person person = Utility.getDaoSession().getPersonDao().load(personId);

		eventId = getIntent().getLongExtra("eventId", -1);
		Event event;
		if (eventId == -1)
			event = new Event();
		else
			event = Utility.getDaoSession().getEventDao().load(eventId);

		String typeCode = getIntent().getStringExtra("eventTypeCode");

		tvPerson = (TextView) findViewById(R.id.tvPerson);
		tvPerson.setText(person.toString());

		tvType = (TextView) findViewById(R.id.tvType);
		tvType.setText(EventService.getType(typeCode));

		lvAttributes = (ListView) findViewById(R.id.lvAttributes);
		lvAttributes.setAdapter(new EventAttributeListAdapter(this,
				android.R.layout.simple_list_item_1, person, event, EventService
						.getAttributes(typeCode)));

		btnSave = (Button) findViewById(R.id.btnSave);
		btnSave.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		EventAttributeListAdapter adapter = (EventAttributeListAdapter) lvAttributes.getAdapter();
		for (int i = 0; i < adapter.getCount(); i++) {
			Object o = adapter.getMixedEvent(i);
			if (o != null) {
				if (o instanceof PersonEvent)
					Utility.getDaoSession().getPersonEventDao().insertOrReplace((PersonEvent) o);
				else if (o instanceof EventDocument)
					Utility.getDaoSession().getEventDocumentDao()
							.insertOrReplace((EventDocument) o);
				else if (o instanceof EventPlace)
					Utility.getDaoSession().getEventPlaceDao().insertOrReplace((EventPlace) o);
				else if (o instanceof EventDate)
					Utility.getDaoSession().getEventDateDao().insertOrReplace((EventDate) o);
				else if (o instanceof EventString)
					Utility.getDaoSession().getEventStringDao().insertOrReplace((EventString) o);
			}
		}
		Utility.getDB().setTransactionSuccessful();
		Utility.getDB().endTransaction();
		Utility.refreshSession();
		setResult(RESULT_OK);
		finish();
	}

	@Override
	public void onBackPressed() {
		Utility.getDB().endTransaction();
		Utility.refreshSession();
		super.onBackPressed();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case EventAttribute.PERSON:
				int position = data.getIntExtra("position", 0);
				long personId = data.getLongExtra("personId", 0);
				Person p = Utility.getDaoSession().getPersonDao().load(personId);
				((TextView) lvAttributes.getChildAt(position).findViewById(R.id.tvValue)).setText(p
						.toString());
				PersonEvent pe = (PersonEvent) ((EventAttributeListAdapter) lvAttributes
						.getAdapter()).getMixedEvent(position);
				if (pe == null) {
					pe = new PersonEvent(null, personId, eventId,
							((EventAttribute) lvAttributes.getItemAtPosition(position))
									.getRoleCode());
					((EventAttributeListAdapter) lvAttributes.getAdapter()).setMixedEvent(position,
							pe);
				}
				pe.setPerson(p);
				break;
			case EventAttribute.PLACE:
				position = data.getIntExtra("position", 0);
				long placeId = data.getLongExtra("placeId", 0);
				Place pl = Utility.getDaoSession().getPlaceDao().load(placeId);
				((TextView) lvAttributes.getChildAt(position).findViewById(R.id.tvValue))
						.setText(pl.toString());
				EventPlace ep = (EventPlace) ((EventAttributeListAdapter) lvAttributes.getAdapter())
						.getMixedEvent(position);
				if (ep == null) {
					ep = new EventPlace(null, eventId, placeId,
							((EventAttribute) lvAttributes.getItemAtPosition(position))
									.getRoleCode());
					((EventAttributeListAdapter) lvAttributes.getAdapter()).setMixedEvent(position,
							ep);
				}
				ep.setPlace(pl);
				break;
			case EventAttribute.DATE:
				// TODO
				break;
			case EventAttribute.STRING:
				// TODO
				break;
			default:
				break;
			}
		}
	}
}
